﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BusinessLogic
{
    public class NhomQuyen
    {
        public int Insert(Entities.NhomQuyen nq)
        {
            SqlConnection con = DataAccess.Connection.OpenConnection();
            int i = 0;
            try
            {
                
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "sp_NhomQuyen_Insert";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;

                cmd.Parameters.Add("@TenNhomQuyen", SqlDbType.NVarChar, 200).Value = nq.TenNhomQuyen;
                cmd.Parameters.Add("@isDeleted", SqlDbType.Bit).Value = nq.isDeleted;

                i = cmd.ExecuteNonQuery();

                LuuVietHeThong.LuuViet("Insert", nq.TK.MaNhanVien, "Thêm nhóm quyền: " + nq.TenNhomQuyen, nq.TK.TenDangNhap, DateTime.Now.ToString("hh:mm dd/MM/yyyy"));
            }
            catch (Exception)
            {

                i = -2;
            }
            finally
            {
                con.Close();
            }
            return i;

        }
        public int Delete(Entities.NhomQuyen nq)
        {
            SqlConnection con = DataAccess.Connection.OpenConnection();
            int i = 0;
            try
            {
                
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "sp_NhomQuyen_Delete";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;

                cmd.Parameters.Add("@TenNhomQuyen", SqlDbType.NVarChar, 200).Value = nq.TenNhomQuyen;

                i = cmd.ExecuteNonQuery();

                LuuVietHeThong.LuuViet("Delete", nq.TK.MaNhanVien, "Xóa nhóm quyền: " + nq.TenNhomQuyen, nq.TK.TenDangNhap, DateTime.Now.ToString("hh:mm dd/MM/yyyy"));
            }
            catch (Exception)
            {

                i = -2;
            }
            finally
            {
                con.Close();
            }
            return i;
        }
        public DataTable Select()
        {
            SqlConnection con = DataAccess.Connection.OpenConnection();
            DataTable dt = new DataTable();
            try
            {
                
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "sp_NhomQuyen_Select";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;

                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();

                adapter.Fill(ds, "NhomQuyen");
                dt = ds.Tables["NhomQuyen"];
            }
            catch
            {
                dt = null;
            }
            finally
            {
                con.Close();
            }
            return dt;
        }
    }
}
